<?php


namespace SidToken\Model;


use Think\Model;

class SidMinerFakebuyModel extends Model
{
    public function fakeBuy($miner_id, $amount, $day_amount, $start_time, $end_time)
    {
        $data = [
            'amount' => $amount,
            'day_amount' => $day_amount,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => 1,
            'create_time' => time()
        ];
        $ret = $this->add($data);
        $Miner = new SidMinerModel();
        $Miner->where(['id' => $miner_id])->setDec('left_amount', $amount);
        return $ret;
    }

    /**
     * 计算全网总产出
     */
    public function calculateTotalFakeMiner()
    {
        $lists = $this->where("status=1")->select();
        $totalAmount = 0;
        foreach ($lists as $val) {
            $dayAmount = $val['day_amount'];
            $amount = $val['amount'];
            $startTime = $val['start_time'];
            $endTime = $val['end_time'];
            $time = strtotime(date("Ymd"));
            if ($time < $startTime) {
                continue;
            }
            if ($time < $endTime) {
                $day = ($time - $startTime) / 86400;
            }else{
                $day = ($endTime - $startTime) / 86400;
            }

            $totalAmount += $dayAmount * $day * $amount;
        }
        return $totalAmount;
    }
}